<?php
/**
 * @version        $Id: search.php 19343 2010-11-03 18:12:02Z ian $
 * @package        Joomla
 * @subpackage    Search
 * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
 * @license        GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

/**
 * @package        Joomla
 * @subpackage    Search
 */
class Mwc_EmployeeModelMwc_Employee extends JModel{

    var $lists = '';

    /**
     * Overridden constructor
     * @access    protected
     */
    function __construct(){
        parent::__construct();
    }

    function reset(){
        $db =& JFactory::getDBO();
        $db->setQuery( 'DELETE FROM #__mwc_employee_dep' );
        $db->query();
    }

    function getItems( ){
        global $mainframe, $option;
        $db    =& JFactory::getDBO();        

        
        $filter_order        = $mainframe->getUserStateFromRequest( 'com_mwc_employee.dep.filter_order',        'filter_order',        '',     'cmd' );
        $filter_order_Dir    = $mainframe->getUserStateFromRequest( 'com_mwc_employee.dep.filter_order_Dir',    'filter_order_Dir',    '',        'word' );
        $limit               = $mainframe->getUserStateFromRequest( 'global.list.limit',                       'limit',            $mainframe->getCfg('list_limit'), 'int' );
        $filter_state        = $mainframe->getUserStateFromRequest( 'com_mwc_employee.dep.filter_state',         'filter_state',        '',            'word' );
        $limitstart          = $mainframe->getUserStateFromRequest( 'com_mwc_employee.dep.limitstart',        'limitstart',        0,        'int' );
        $search              = $mainframe->getUserStateFromRequest( 'com_mwc_employee.dep.search',            'search',            '',        'string' );
        if (strpos($search, '"') !== false) {
            $search = str_replace(array('=', '<'), '', $search);
        }
        $search = JString::strtolower($search);

        if (!in_array(strtoupper($filter_order_Dir), array('ASC', 'DESC'))) {
            $filter_order_Dir = '';
        }

        // table ordering
        if ( strtoupper($filter_order_Dir) == 'ASC' ) {
            $this->lists['order_Dir'] = 'ASC';
        } else {
            $this->lists['order_Dir'] = 'DESC';
        }
        $this->lists['order'] = $filter_order;

        // search filter
        $this->lists['search']= $search;
        
        // state filter
        $this->lists['state']    = JHTML::_('grid.state',  $filter_state ); 
        
        $where = array();
        if ( $search ) {
            $where[] = 'name LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
        }
        $where = ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );

        if($filter_order)
            $orderby     = ' ORDER BY '. $filter_order .' '. $filter_order_Dir;

        // get the total number of records
        $query = 'SELECT COUNT(*)'
        . ' FROM #__mwc_employee_dep '
        . $where;
        $db->setQuery( $query );     
        $total = $db->loadResult();

        jimport( 'joomla.html.pagination' );
        $pageNav = new JPagination( $total, $limitstart, $limit );

        $query = ' SELECT * ' .
            ' FROM #__mwc_employee_dep ' .
            $where .
            $orderby;             
        
        $db->setQuery( $query, $pageNav->limitstart, $pageNav->limit );
        $rows = $db->loadObjectList();

        return $rows;
    }
    
    function getItem( ){
        global $mainframe, $option;
        
        $db     =& JFactory::getDBO();   
        $cid    = JRequest::getVar('cid', array(0), '', 'array');
        JArrayHelper::toInteger($cid, array(0));
        
        $row =& JTable::getInstance('mwc_employeedep', 'Table');
        // load the row from the db table
        if($cid[0] > 0)
            $row->load( $cid[0] );
    
        $this->lists['published'] = JHTML::_('select.booleanlist',  'published', '', $row->published ); 
                    
        return $row;
    }
}
